package com.ct.gd.passport.domain;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.ct.gd.passport.common.connect.CtServer;
import com.ct.gd.passport.common.constants.Config;
import com.ct.gd.passport.common.constants.Protocol;
import com.ct.gd.passport.util.TelephonyState;
import com.fuckhtc.gson.Gson;
import com.fuckhtc.gson.annotations.SerializedName;
import com.fuckhtc.gson.reflect.TypeToken;
import com.up91.common.android.connect.Params;
import com.up91.common.android.exception.NullDataException;

public class AdviceInfo {

	// 最新咨询信息
	public static int ADVICE_NEW_LIST = 1;
	// 我提交的咨询信息
	public static int ADVICE_MY_LIST = 2;

	@SerializedName("id")
	private int id;

	@SerializedName("adviceTitle")
	private String title;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public static List<AdviceInfo> load(int type, int pageIndex, int pageSize) {
		Params params = new Params();
		params.put(Protocol.Fields.PAGE_INDEX, pageIndex);
		params.put(Protocol.Fields.PAGE_SIZE, pageSize);
		params.put(Protocol.Fields.ADVICE_LIST_TYPE, type);
		params.put(Protocol.Fields.IMEI, TelephonyState.getInstance().getIMEI());
//		params.put(Protocol.Fields.USER_PHONE, TelephonyState.getInstance().getPhoneNumber());

		if (Config.ON_LINE) {
			String json = CtServer.getServer().doGet(
					Protocol.Commands.ADVICE_LIST, params);

			try {
				JSONObject jObj = new JSONObject(json);

				TypeToken<List<AdviceInfo>> typeToken = new TypeToken<List<AdviceInfo>>() {
				};
				return new Gson().fromJson(
						jObj.getString(Protocol.Fields.DATA),
						typeToken.getType());
			} catch (JSONException e) {
				e.printStackTrace();
				throw new NullDataException();
			}
		}

		List<AdviceInfo> infos = new ArrayList<AdviceInfo>();
		AdviceInfo info = null;
		for (int i = 0; i < 20; i++) {
			info = new AdviceInfo();
			info.setId(i);
			info.setTitle("第" + i + "条咨询");
			infos.add(info);
		}
		return infos;
	}


	public static boolean submit(String type,String title,String content,String contact,String contactPhone) {
		Params params = new Params();
//		params.put(Protocol.Fields.ADVICE_CONTENT, changeCharset(content,"UTF-8"));
//		params.put(Protocol.Fields.ADVICE_TITLE, changeCharset(title,"UTF-8"));
		params.put(Protocol.Fields.ADVICE_TYPE,Integer.parseInt(type));
		params.put(Protocol.Fields.ADVICE_TITLE, title);
		params.put(Protocol.Fields.ADVICE_CONTENT,content);
		params.put(Protocol.Fields.ADVICE_CONTACT,contact);
		params.put(Protocol.Fields.ADVICE_CONTACT_PHONE,contactPhone);
//		params.put(Protocol.Fields.USER_PHONE, TelephonyState.getInstance().getPhoneNumber());
		if (Config.ON_LINE) {
			String json = CtServer.getServer().doGet(
					Protocol.Commands.ADVICE_SUBMIT, params);
			return new Gson().fromJson(json, Boolean.class);
		}
		return true;
	}
}
